﻿Public Partial Class UploadFile
    Inherits System.Web.UI.Page

    Dim clsGet As New ClassFunctionGetData
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not (IsPostBack) Then
            gridfile.PageIndex = 0
            gridfile.DataSource = Session("Q88")
            gridfile.DataBind()
        End If
    End Sub

    Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " parent.closeModal('U');", True)
    End Sub

    Private Sub btnClose_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClose.Click
        ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " parent.closeModal('C');", True)
    End Sub

    Public Function CreateFileDataTable() As DataTable
        Dim dt As New DataTable
        dt.Columns.Add("File_nameGuid", GetType(String))
        dt.Columns.Add("File_name", GetType(String))
        dt.Columns.Add("File_Type", GetType(String))
        dt.Columns.Add("File_ContentType", GetType(String))
        dt.Columns.Add("Upload_Type", GetType(String))
        dt.Columns.Add("Description", GetType(String))
        dt.AcceptChanges()
        Return dt
    End Function

    Private Sub btnQ88_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnQ88.Click
        Try
            If (FileUpload1.PostedFile.FileName <> "") Then
                Dim filename As String = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName) 'ชื่อไฟล์
                Dim Type As String = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName)   'นามสกุล
                Dim ContentType As String = FileUpload1.PostedFile.ContentType
                Dim nameGuid As String = Guid.NewGuid().ToString()
                FileUpload1.SaveAs(Server.MapPath("../Uploads/") + nameGuid + Type)
                Dim dt As DataTable = Session("Q88")
                If dt Is Nothing Then
                    dt = CreateFileDataTable()
                End If
                If (dt.Rows.Count > 0) Then
                    For Each drx As DataRow In dt.Rows
                        If (drx.Item("Upload_Type") = "Q88") Then
                            System.IO.File.Delete(Server.MapPath("../Uploads/") + drx.Item("File_nameGuid"))
                            drx.Delete()
                            Exit For
                        End If
                    Next
                End If
                dt.AcceptChanges()
                Dim dr As DataRow = dt.NewRow()
                dr.Item("File_nameGuid") = nameGuid
                dr.Item("File_name") = filename.Split(".")(0)
                dr.Item("File_Type") = Type
                dr.Item("File_ContentType") = ContentType
                dr.Item("Upload_Type") = "Q88"
                dr.Item("Description") = txtRemarkQ88.Text
                txtRemarkQ88.Text = String.Empty
                dt.Rows.Add(dr)
                dt.AcceptChanges()
                Session("Q88") = dt
                gridfile.DataSource = dt
                gridfile.DataBind()
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('Upload ไฟล์เสร็จสมบูรณ์ !!');", True)
            Else
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการเลือกไฟล์ Q88 ที่ต้องการทำการ Upload !!');", True)
            End If
        Catch ex As Exception
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('เกิดความผิดพลาดในการ Upload ไฟล์ !!');", True)
        End Try
        
    End Sub

    Private Sub btnOther_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOther.Click
        Try
            If (FileUpload2.PostedFile.FileName <> "") Then
                If (FileUpload2.PostedFile.ContentLength > 2048000) Then
                    ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('ไฟล์ที่อัพโหลดมีขนาดเกิน 2 MB !!');", True)
                    Exit Sub
                End If
                Dim filename As String = System.IO.Path.GetFileName(FileUpload2.PostedFile.FileName) 'ชื่อไฟล์
                Dim Type As String = System.IO.Path.GetExtension(FileUpload2.PostedFile.FileName)   'นามสกุล
                Dim ContentType As String = FileUpload2.PostedFile.ContentType
                Dim nameGuid As String = Guid.NewGuid().ToString()
                FileUpload2.SaveAs(Server.MapPath("../Uploads/") + nameGuid + Type)
                Dim dt As DataTable = Session("Q88")
                If dt Is Nothing Then
                    dt = CreateFileDataTable()
                End If
                Dim dr As DataRow = dt.NewRow()
                dr.Item("File_nameGuid") = nameGuid
                dr.Item("File_name") = filename.Split(".")(0)
                dr.Item("File_Type") = Type
                dr.Item("File_ContentType") = ContentType
                dr.Item("Upload_Type") = "เอกสารประกอบ"
                dr.Item("Description") = txtRemarkOther.Text
                txtRemarkOther.Text = String.Empty
                dt.Rows.Add(dr)
                dt.AcceptChanges()
                Session("Q88") = dt
                gridfile.DataSource = dt
                gridfile.DataBind()
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('Upload ไฟล์เสร็จสมบูรณ์ !!');", True)
            Else
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการเลือกไฟล์ที่ต้องการทำการ Upload !!');", True)
            End If
        Catch ex As Exception
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('เกิดความผิดพลาดในการ Upload ไฟล์ !!');", True)
        End Try
    End Sub

    Private Sub gridfile_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gridfile.RowCommand
        Dim Index As Integer = Convert.ToInt32(e.CommandArgument)
        If (e.CommandName = "cmddelete") Then
            Dim dt As DataTable = Session("Q88")
            System.IO.File.Delete(Server.MapPath("../Uploads/") + dt.Rows(Index).Item("File_nameGuid"))
            dt.Rows(Index).Delete()
            dt.AcceptChanges()
            Session("Q88") = dt
            gridfile.DataSource = dt
            gridfile.DataBind()
        End If
    End Sub

    Private Sub MainPage_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        Dim ds As DataSet = Session("UserData")
        If Not (ds Is Nothing) Then
            Page.MasterPageFile = clsGet.GetMaster_Page(ds.Tables(0).Rows(0).Item("User_Name").ToString(), "")
        End If
    End Sub
End Class